#include "FlameSensor.h"

FlameSensor::FlameSensor(uint8_t doPin) 
    : _doPin(doPin) {}

void FlameSensor::begin() {
    if(_doPin > 39) { // ESP32引脚有效性检查
        Serial.printf("错误: 无效的火焰传感器引脚 %d\n", _doPin);
        while(1); // 停止执行
    }
    pinMode(_doPin, INPUT_PULLUP);
    Serial.printf("火焰传感器初始化完成 (引脚: %d)\n", _doPin);
}

bool FlameSensor::isFlameDetected() const {
    static uint32_t lastTime = 0;
    static bool lastState = false;
    
    uint32_t now = millis();
    if(now - lastTime < DEBOUNCE_DELAY) {
        return lastState; // 消抖期内返回上次状态
    }
    
    bool current = (digitalRead(_doPin) == LOW);
    lastTime = now;
    lastState = current;
    
    return current;
}
